/**
 * Copyright 2008 Martin 'Windgazer' Reurings
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.windgazer.maven.plugins.jsdoc;

import java.io.File;
import java.util.Locale;

import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;

/** 
 * Run JSDoc on the current webapp. This Report contains a version of
 * JSDoc Tooklit and wraps around it to be able to run it using Maven 2.<br />
 * Developers have access to all the 'options' that JSDoc has and
 * they can be overridden using mavens' plugin configuration.<br />
 * <br />
 * See the <code>JSDocGenerator</code> for configuration options.<br />
 * In addition to the jsdoc toolkit properties, two more properties
 * are available for this goal to influence the site:site goal:<br />
 * <pre>
 *		&lt;name>jsdoc&lt;/name>
 *		&lt;description>This is a JavaDoc like report for the javascript files in this project&lt;/description>
 * </pre>
 *
 * @see JSDocGenerator
 * @author mreuring
 * @goal jsdoc-report
 * @phase site
 */
public class JSDocReport extends AbstractMavenReport {

	/**
	 * <i>Maven Internal</i>: The Project descriptor.
	 * @parameter expression="${project}"
	 * @required
	 * @readonly
	 */
	private MavenProject project;
	 
	/**
	 * <i>Maven Internal</i>: The Doxia Site Renderer.
	 *
	 * @component
	 */
	private Renderer siteRenderer;

	/**
     * The temporary directory where JSDoc toolkit is stored while running.
     * This is not a JSDoc property, and won't be found in the JSDoc documentation.
     *
     * @parameter expression="${project.build.directory}/toolkit"
     */
	private File tempDir;
	
	/**
     * Template used to format the output. The opions are 'htm', 'sunny' or 'xml'.
     * This property represents: -t=<PATH> or --template=<PATH>
     *
     * @parameter expression="jsdoc"
     */
	private String template;
	
	/**
     * The target directory where you want the generated doc to end up. This
     * property represents: -d=&lt;PATH> or --directory=&lt;PATH>.
     * Changing this to a path outside the reporting outputdir will invalidate
     * the site, the link to this report will be broken. Also, the current
     * reporter wont work with subdirs, it'll simply asume that you're generating
     * a report in a dir directly of the root.
     *
     * @parameter expression="${project.reporting.outputDirectory}/jsdoc"
     */
	private File directory;
	
	/**
	 * The level of recursion. Default is 1.
	 * 
	 * @parameter expression="1"
	 */
	private int recurse;
	
	/**
	 * The extension for the javascript files, default is 'js'.
	 * 
	 * @parameter expression="js"
	 */
	private String extension;
	
	/**
	 * Include all functions in the JSDoc, including undocumented ones.
	 * 
	 * @parameter expression="true"
	 */
	private boolean allFunctions;

	/**
	 * Include all functions in the JSDoc, including undocumented and underscored ones.
	 * 
	 * @parameter expression="false;
	 */
	private boolean AllFunctions;
	
	/**
	 * Include symbols tagged as private.
	 * 
	 * @parameter expression="false"
	 */
	private boolean privateOption;

	
	/**
	 * The directory where we may find the source-files we're making jsdocs for.
	 * This property represents: &lt;SRC_DIR>
	 * 
	 * @parameter expression="${basedir}/src/main/webapp"
	 */
	private File srcDir; //TODO: Find the expression for the current project-dir!
	
	/**
	 * The name with which this report will be displayed in the site:site results.
	 * 
	 * @parameter expression="jsdoc"
	 */
	private String name;
	
	/**
	 * The description with which this report will be shown in the site:site results.
	 * 
	 * @parameter expression="This is a JavaDoc like report for the javascript files in this project"
	 */
	private String description;
	
	/**
	 * 
	 * @parameter
	 */
	private String[] exclude = null;


	/**
	 * Execute the mojo, this is what mvn calls to start this mojo.
	 */
	@Override
	protected void executeReport(Locale arg0) throws MavenReportException {

		Locale locale = Locale.getDefault();
		try {
			JSDocGenerator gen = new JSDocGenerator(tempDir, directory, srcDir, exclude, template, extension, recurse, allFunctions, AllFunctions, privateOption, getLog());
			gen.generateReport(locale);
		} catch (JSDocException e) {
			throw new MavenReportException(e.getLocalizedMessage());
		}

		//Renaming index.htm to index.html, otherwise the site:site goal won't link properly...
		File index = new File(directory, "index.htm");
		if (index.exists()) index.renameTo(new File(directory, "index.html"));

	}

	@Override
	protected String getOutputDirectory() {
		return directory.getAbsolutePath();
	}

	@Override
	protected MavenProject getProject() {
		return project;
	}

	@Override
	protected Renderer getSiteRenderer() {
		return siteRenderer;
	}

	public String getDescription(Locale arg0) {
		return description;
	}

	public String getName(Locale arg0) {
		return name;
	}

	public String getOutputName() {
		return directory.getName() + "/index";
	}

	public boolean isExternalReport() {
	         return true;
	}

}
